/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.StockCode;
import com.openbrew.openbrewrf.Supplies;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import tools.InventoryModifiers;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.supplies")
public class SuppliesFacadeREST extends AbstractFacade<Supplies> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public SuppliesFacadeREST() {
        super(Supplies.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public Supplies create(@WebParam(name = "supplies") Supplies entity) {
        //Add stock code if it is set.
        if(entity.getStockCode() != null){
            StockCode sc = entity.getStockCode();
            getEntityManager().persist(sc);
            getEntityManager().flush();
            getEntityManager().refresh(sc);
            entity.setStockCode(sc);
            entity = super.create(entity);
            sc.setTableID(entity.getSupID());
            getEntityManager().merge(sc);
        } else {
            entity = super.create(entity);
        }
        return entity;
    }

    @POST
    @Path("updateAllSupplies")
    @Consumes({"application/xml", "application/json"})
    public void updateHopList(List<Supplies> entities) {
        if(entities != null){
            for(Supplies entity : entities){
                super.edit(entity);
            }
        }
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Supplies entity) {
        if(entity.getStockCode()!=null){
            getEntityManager().merge(entity.getStockCode());
            super.edit(entity);
        } else {
            //insert stock code joiner first.
            super.edit(entity);
        }
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Supplies find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Supplies> findAll() {
        return super.findAll();
    }

    @GET
    @Path("runSupplyAllocationFix")
    @Produces({"application/xml", "application/json"})
    public List<Supplies> runSAFAllocationFix() {
        final InventoryModifiers im = new InventoryModifiers();
        im.runSupplyAllocationFix(em);
        return em.createNamedQuery("Supplies.findAll").getResultList();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Supplies> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }
    
    @GET
    @Path("runNamedQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Supplies> runNamedQuery(@PathParam("str") String str){
        return super.runNamedQuery(str);
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
